package net.jalbum.filters.arty;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import net.jalbum.filters.arty.effects.ArtySupport;
import se.datadosen.component.MarginIcon;
import se.datadosen.jalbum.JAFilter;
import se.datadosen.util.PersistentList;

/**
 *
 * @author Sarah Schölzel
 */
public class ArtySelector extends JPanel implements PersistentList {

    public static final String EFFECT_PROPERTY = "effect";
    private Color highColor = SystemColor.textHighlight;
    private BufferedImage image;
    private ButtonGroup group = new ButtonGroup();
    private Effect selectedEffect = Effect.none; // ActionPerformed can't keep track of last selected Effect

    /**
     * Constructor
     * @param plugin 
     */
    public ArtySelector() {
        try {
            this.image = ImageIO.read(this.getClass().getResource("res/sample.jpg"));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

        initComponents();

    }

    public ArtySelector(BufferedImage bi) {
        this.image = bi;
        initComponents();
    }

    /**
     * adds thumbnails to the panel
     */
    private void initComponents() {
        setLayout(new GridLayout(3, 3, 2, 2));

        ActionListener buttonListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Effect oldEffect = selectedEffect;
                JRadioButton source = (JRadioButton) e.getSource();
                selectedEffect = Effect.valueOf(source.getName());
                firePropertyChange(EFFECT_PROPERTY, oldEffect, selectedEffect);
            }
        };

        Insets insets = new Insets(4, 4, 4, 4);

        for (Effect f : Effect.values()) {
            BufferedImage dstImg = image;
            JRadioButton btn = new JRadioButton();
            if (f != Effect.none) {
                JAFilter effectFilter = f.getFilter();
                dstImg = effectFilter.filter(image, null);
                if (f.hasBorder()) {
                    dstImg = ArtySupport.adjustBorder(dstImg);
                }
            } else {
                btn.setSelected(true);
            }
            btn.setName(f.name());
            btn.setToolTipText(f.toString() + ": " + f.getToolTip());
            btn.setIcon(new MarginIcon(insets, new ImageIcon(dstImg)));
            btn.setSelectedIcon(getHighligthedIcon(btn));
            btn.setBorder(BorderFactory.createEmptyBorder());
            btn.addActionListener(buttonListener);
            group.add(btn);
            add(btn);
        }

    }

    /**
     * draws a rectangle border to see when a image is selected
     * @param bi image which is selected 
     * @return highlighted image
     */
    private Icon getHighligthedIcon(JRadioButton button) {
        Icon icon = button.getIcon();
        BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bi.createGraphics();
        icon.paintIcon(button, g, 0, 0);

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
        g.setColor(highColor);
        g.setStroke(new BasicStroke(4.0f));
        g.drawRect(2, 2, bi.getWidth() - 5, bi.getHeight() - 5);
        g.dispose();
        return new ImageIcon(bi);
    }

    public Effect getSelectedEffect() {
        return selectedEffect;
    }

    public void setselectedEffect(Effect effect) {
        Effect oldEffect = getSelectedEffect();
        if (effect == oldEffect) {
            return;
        }
        for (Component comp : getComponents()) {
            if (comp.getName().equals(effect.name())) {
                ((JRadioButton) comp).setSelected(true);
                selectedEffect = effect;
                break;
            }
        }
        firePropertyChange(EFFECT_PROPERTY, oldEffect, effect);
    }

    @Override
    public List<String> getContent() {
        List<String> theList = new ArrayList<String>();
        theList.add(getSelectedEffect().name());
        return theList;
    }

    @Override
    public void setContent(List<String> list) {
        String effect = list.get(0);
        if ((effect == null) || effect.equals("")) {
            setselectedEffect(null);
        } else {
            try {
                setselectedEffect(Effect.valueOf(list.get(0)));
            } catch (Exception ex) {
                System.err.println("ArtySelector ignoring illegal effect: " + ex.toString());
//                throw new RuntimeException(ex);
            }
        }
    }
}
